/*
 * fbank.com Inc.
 * Copyright (c) 2020 All Rights Reserved.
 * create by fm
 * date:2020-04-26
 */
package com.ouwen.smartpay.service.impl;

import com.fm.common.emuns.MessageType;
import com.fm.core.common.boot.Apps;
import com.fm.core.common.facade.MerchOrderBase;
import com.fm.core.common.service.EntityServiceImpl;
import com.fm.core.utils.Strings;
import com.ouwen.smartpay.dao.OrderInfoDao;
import com.ouwen.smartpay.entity.OrderDetail;
import com.ouwen.smartpay.entity.OrderInfo;
import com.ouwen.smartpay.entity.Partner;
import com.ouwen.smartpay.openapi.order.ServiceOrder;
import com.ouwen.smartpay.service.OrderDetailService;
import com.ouwen.smartpay.service.OrderInfoService;
import com.ouwen.smartpay.service.PartnerService;
import com.ouwen.smartpay.utils.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 统一订单流水 Service实现
 * <p>
 * Date: 2020-04-26 10:59:41
 *
 * @author fm
 */
@Service("orderInfoService")
public class OrderInfoServiceImpl extends EntityServiceImpl<OrderInfo, OrderInfoDao> implements OrderInfoService {

    @Resource
    private OrderInfoDao orderInfoDao;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private OrderDetailService orderDetailService;

    @Override
    public OrderInfo findOderInfoByPartnerIdAndRequestNoService(OrderInfo orderInfo) {
        return orderInfoDao.findOderInfoByPartnerIdAndRequestNoService(orderInfo);
    }

    @Override
    public OrderInfo findOrderInfoByPartnerIdAndAndSubParentIdRequestNo(String partnerId, String subPartnerId, String requestNo) {
        return orderInfoDao.findOrderInfoByPartnerIdAndSubPartnerIdRequestNo(partnerId, subPartnerId, requestNo);
    }

    @Override
    public OrderInfo findOrderInfoByPartnerIdAndMerchOrderNo(String partnerId, String subPartnerId, String merchOrderNo) {
        return orderInfoDao.findOrderInfoByPartnerIdAndSubPartnerIdAndMerchOrderNo(partnerId, subPartnerId, merchOrderNo);
    }

    @Transactional
    @Override
    public void saveOrderInfo(ServiceOrder serviceOrder, MerchOrderBase merchOrder) {

        OrderInfo orderInfo = new OrderInfo();

        orderInfo.setPartnerId(merchOrder.getPartnerId());

        orderInfo.setSubPartnerId(merchOrder.getSubPartnerId());

        orderInfo.setGid(merchOrder.getGid());

        orderInfo.setRequestNo(serviceOrder.getRequestNo());

        orderInfo.setServiceName(serviceOrder.getService());

        orderInfo.setServiceVersion(serviceOrder.getVersion());

        orderInfo.setServiceGroup(serviceOrder.getGroup());

        MessageType messageType = serviceOrder.getMessageType();

        if (messageType == null) {

            //查询商户接入配置
            PartnerService partnerService = Apps.getApplicationContext().getBean(PartnerService.class);

            Partner partner = partnerService.findPartner(serviceOrder.getPartnerId(), serviceOrder.getSubPartnerId());

            if (partner != null) {

                messageType = partner.getMessageType();

            }

        }

        orderInfo.setMessageType(messageType);

        orderInfo.setMerchOrderNo(merchOrder.getMerchOrderNo());

        orderInfo.setCharset("UTF-8");

        String notifyUrl = serviceOrder.getNotifyUrl();

        if (Strings.isBlank(notifyUrl)) {

            //查询商户接入配置
            PartnerService partnerService = Apps.getApplicationContext().getBean(PartnerService.class);

            Partner partner = partnerService.findPartner(serviceOrder.getPartnerId(), serviceOrder.getSubPartnerId());

            if (partner != null) {

                notifyUrl = partner.getNotifyUrl();

            }

        }

        orderInfo.setNotifyUrl(notifyUrl);

        //保存订单信息
        orderInfoService.save(orderInfo);

        //对请求体进行持久化
        OrderDetail orderDetail = orderDetailService.findOrderDetailRequestInfoByOrderId(orderInfo.getId(), orderInfo.getPartnerId(), orderInfo.getSubPartnerId());

        //请求原始数据转换为json
        String result = BeanUtil.mapToJson(serviceOrder.getDatas());

        if (orderDetail == null) {

            OrderDetail _orderDetail = new OrderDetail();

            _orderDetail.setOrderId(orderInfo.getId());

            _orderDetail.setRequestInfo(result);

            //写入数据
            orderDetailService.saveOrderDetailAndCache(orderInfo.getPartnerId(), orderInfo.getSubPartnerId(), _orderDetail);

        } else {

            orderDetail.setRequestInfo(result);

            //更新数据和失效缓存
            orderDetailService.updateOrderDetail(orderInfo.getPartnerId(), orderInfo.getSubPartnerId(), orderDetail);

        }
    }
}
